﻿using System.Diagnostics;

namespace Codemonk.Common.Configuring
{
    /// <summary>
    /// 程序集类型的配置文件，这种配置文件是用来给整个组件使用的
    /// </summary>
    public class AssemblyConfigReader : ConfigReader, IConfigReader, IConfigurable
    {
        public AssemblyConfigReader()
        {
            Reader = new ConfigReader { ConfigFile = "./config/codemonk.common.config.xml" };
            var frame = new StackFrame(1);
            assemblyName = frame.GetMethod().DeclaringType.Assembly.GetName().Name.ToLower();
            Initialization();
        }

        private string assemblyName;
        private string pathFormat;

        public IConfigReader Reader { get; set; }

        public void Initialization()
        {
            if (Reader == null) return;
            try
            {
                pathFormat = Reader.ReadFirst("/codemonk/configpatterns/configpattern[@name='assembly']/@pattern");
                ConfigFile = string.Format(pathFormat, assemblyName);
            }
            catch { throw AssemblyConfigException.Instance; }
        }
    }
}
